package com.os.sys.service;

import com.os.sys.common.utility.StringClass;
import com.os.sys.common.utility.XJsonInfo;
import com.os.sys.model.T_SYS_Role;
import com.os.sys.model.T_SYS_Role_To_User;
import com.os.sys.repository.RoleRepository;
import com.os.sys.repository.Role_To_UserRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * Created by Administrator on 2017/5/27.
 */
@Service
public class RoleService {
    @Resource
    private RoleRepository roleRepository;
    @Resource
    private Role_To_UserRepository role_to_userRepository;

    public List<T_SYS_Role> getRolesByUserId(Long userId) {
        List<Long> listR_T_U = role_to_userRepository.findUserRoleIds(userId);
        List<T_SYS_Role> listRoles = roleRepository.findByIdIn(listR_T_U);

        return listRoles;
    }

    public List<Long> getRolesIDsByUserId(Long userId) {
        List<T_SYS_Role> list = getRolesByUserId(userId);
        return list.stream().map(q -> q.getId()).collect(toList());
    }

    public List<String> getRolesPowerListByUserId(Long userId) {
        List<T_SYS_Role> list = getRolesByUserId(userId);
        List<String> powerList = new ArrayList<>();
        list.stream().forEach(q -> powerList.addAll(Arrays.asList(q.getPower().split(","))));

        return powerList.stream().distinct().collect(toList());
    }

    public List<String> getRolesKeysByUserId(Long userId) {

        List<Long> listR_T_U = role_to_userRepository.findUserRoleIds(userId);

        if (listR_T_U.size() == 0) {
            return new ArrayList<>();
        }
        List<T_SYS_Role> listRoles = roleRepository.findByIdIn(listR_T_U);
        return listRoles.stream().map(q -> q.getRoleKey()).collect(toList());
    }

    public List<String> getRolesPowersByUserId(Long userId) {
        List<T_SYS_Role> list = getRolesByUserId(userId);
        return list.stream().map(q -> q.getPower()).distinct().collect(toList());
    }

    public XJsonInfo updateUserRoles(Long userId, String roleIds) {
        List<T_SYS_Role_To_User> listR_T_U = role_to_userRepository.findAllByUserId(userId);

        // 新的添加，旧的删除
        List<String> arrRoleIds = new ArrayList(Arrays.asList(roleIds.split(",")));
        List<T_SYS_Role_To_User> addList = new ArrayList<>();
        List<T_SYS_Role_To_User> removeList = new ArrayList<>();
        for (T_SYS_Role_To_User dbInfo : listR_T_U) {
            String roleId = dbInfo.getRoleId().toString();
            if (arrRoleIds.contains(roleId)) {
                arrRoleIds.remove(roleId);
            } else {
                removeList.add(dbInfo);
            }
        }

        // 剩下的就是数据库里没有的，则是新的
        for (String roleId : arrRoleIds) {
            T_SYS_Role_To_User info = new T_SYS_Role_To_User();
            info.setRoleId(Long.valueOf(roleId));
            info.setUserId(userId);
            info.setUserType(0);
            addList.add(info);
        }
        role_to_userRepository.deleteAll(removeList);
        role_to_userRepository.saveAll(addList);
        return new XJsonInfo();
    }

    public T_SYS_Role getModelByKey(String key) {

        if (StringClass.isBlank(key)) {
            return null;
        }

        return roleRepository.findByRoleKey(key);
    }

    public List<String> getRolePowerByRoleKey(String key) {
        T_SYS_Role rInfo = getModelByKey(key);
        if (rInfo == null) {
            return null;
        }
        return rInfo.getPower() == null ? null : Arrays.asList(rInfo.getPower().split(","));
    }

    private int changeUserType(String userType) {
        int t = 0;
        switch (userType) {
            case "local":
            case "portal":
                t = 1;
                break;
            case "os":
                t = 2;
                break;
        }
        return t;
    }
}
